package com.edu.servlet;

import java.io.IOException;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.edu.bean.User;
import com.edu.service.UserService;
import com.edu.service.impl.UserServiceImpl;

@WebServlet("/register.do")
public class RegisterServlet extends HttpServlet{

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) 
			throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		UserService us = new UserServiceImpl();
		String username = request.getParameter("username");
		String password = request.getParameter("password");
		String realName = request.getParameter("realName");
		String sex = request.getParameter("sex");
		String birthday = request.getParameter("birthday");
		DateFormat dft = new SimpleDateFormat("yyyy-MM-dd");
		Date br = null;
		try {
			br = dft.parse(birthday);
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		User user = new User(username,password,realName,sex,br);
		int result = us.userRegister(user);
		if(result == -1){
			request.setAttribute("msg","用户名已经存在");
			request.getRequestDispatcher("register.jsp").forward(request,response);
		}else{
			response.sendRedirect("login.jsp");
		}
	}
}
